//
//  KKStatusViewCell.swift
//  KKWeibo
//
//  Created by kassia on 16/7/14.
//  Copyright © 2016年 kassia. All rights reserved.
//

import UIKit
import SnapKit
/// 三大块视图
let originalMagrinCell: CGFloat = 8

class KKStatusViewCell: UITableViewCell {

    //  记录toolbar顶部约束
    var toolbarTopConstranint: Constraint?
    
    //设置微博数据
    var statusViewModel: KKStatusViewModel? {
        didSet{
        
            //原创微博
            originalView.statusViewModel = statusViewModel
            
            //卸载约束toolbar
            self.toolbarTopConstranint?.uninstall()
            
            //判断是否有转发视图
            if statusViewModel?.status?.retweeted_status != nil {
            
                retweetView.statusViewModel = statusViewModel
                
                retweetView.hidden = false
                
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    
                  self.toolbarTopConstranint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            
            } else {
            
                retweetView.hidden = true
                
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.toolbarTopConstranint = make.top.equalTo(originalView.snp_bottom).constraint
                })

                //  给toolbar视图设置需要的视图模型
                toolBarView.statusViewModel = statusViewModel

            }
        }
    
    }
    
    /**
     *  懒加载 控件
     */
    private lazy var originalView: KKStatusOriginalViewCell = KKStatusOriginalViewCell()
    private lazy var toolBarView: KKStatusToolBar = KKStatusToolBar()
    private lazy var retweetView: KKStatusRetweetView = KKStatusRetweetView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //UI视图
    private func setUpUI() {
        
        // 设置contentView的背景颜色
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        //添加到视图
        contentView.addSubview(originalView)
        contentView.addSubview(toolBarView)
        contentView.addSubview(retweetView)
        //originalView.backgroundColor = RandomColor()
        
        originalView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentView).offset(8)
            make.trailing.equalTo(contentView)
            make.leading.equalTo(contentView)
            
                
        }
        
        //retweetView.backgroundColor = UIColor(white: 0.95, alpha: 1)

        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(originalView).offset(originalMagrinCell)
            make.trailing.equalTo(originalView).offset(-originalMagrinCell)
            
        }
        
        toolBarView.snp_makeConstraints { (make) -> Void in
            
            self.toolbarTopConstranint =  make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        //  非常重要的约束,确定cell的高度, = contentView的底部= toolbar底部约束
        //  约束一定建全

        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(toolBarView)
        }
        
    }



}
